package com.gitee.string.address;

/**
 * @author ming.yan-1
 * @version V1.0
 * @Title com.gitee.string.address
 * @Description
 * @date 2018/9/5  下午12:34
 */
public class Address {
    public static void main(String[] args) {
        String str = "str";
        String str1 = "str";//先从常量池里找，没有就创建并存入常量池，效率高
        System.out.println(str == str1);//true
        String str2 = "strstr";
        String strstr = str+str1;//也是对象在堆内存，不在常量池里
        System.out.println(str2 == strstr);//false;通过String strstr = str+str1;得到的strstr和String str2 = "strstr";得到的str2是否都从常量池中取
        String newStr = new String("str");
        System.out.println(str == newStr);//new的和直接赋值的区别，一个是在常量池里，一个是在堆里
        String newStr2 = new String("str");//newStr存储的是new的对象在堆中的地址
        System.out.println(newStr == newStr2);//false；堆里的不同地址
        StringBuffer stringBuffer = new StringBuffer().append("s").append("t").append("r").append("sb");
        String strsbuffer = stringBuffer.toString();
        StringBuilder stringBuilder = new StringBuilder().append("s").append("t").append("r").append("sb");
        String strBuilder = stringBuilder.toString();
        System.out.println(stringBuffer.toString()==stringBuffer.toString());//两次toString地址不同
        System.out.println(stringBuilder.toString()==stringBuffer.toString());//false
        String sbsb = "strsb";
        System.out.println(stringBuffer.toString()==sbsb);//false
        System.out.println(stringBuilder.toString()==sbsb);//false

        String change = "str";
        System.out.println(change==str);//true,同一常量
        change = "change";
        System.out.println(change==str);//final不可变,是个新的常量
    }
}
